package game.view.menu
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import game.UIComponent.OrderSquare;
	import game.UIComponent.Panel;
	import game.UIComponent.TabButton;
	import game.common.CustomBitmap;
	import game.managers.LanguageManager;
	import game.utils.Utils;
	
	public class MenuPanel extends Panel
	{	
		private var _firstBtn:TabButton;
		private var _secondBtn:TabButton;
		private var _otherBtn:TabButton;
		private var _cateringBtn:TabButton;
		
		private var _remainTimetf:TextField;
		private var _cointf:TextField;
		private var _profit:Number=1920;
		
		private var _orderSquare1:OrderSquare;
		private var _orderSquare2:OrderSquare;
		private var _orderSquare3:OrderSquare;
		
		private var _first:FirstFoodType;
		private var _second:SecondFoodType;
		private var _other:OtherType;
		private var _catering:CateringType;
		
		private var _emptySquareArr:Array=[];
		private var _sourceArr:Array=[null,null,null];
		
		public function MenuPanel()
		{
			init();
		}
		
		override protected function init():void
		{
			setBgImage("menuPanelBg",620,370);
			setCenter();			
			close=new Point(560,-10);
			setImageTitle("learnfoodtxt");

			_firstBtn=new TabButton(LanguageManager.getWord("menu.yue"),clickEvent);
			_firstBtn.x=20;
			_firstBtn.y=30;
			_firstBtn.choose=true;
			_now=_firstBtn;
			addChild(_firstBtn);
			
			_secondBtn=new TabButton(LanguageManager.getWord("menu.chuan"),clickEvent);
			_secondBtn.x=_firstBtn.x + _firstBtn.width;
			_secondBtn.y=_firstBtn.y;
			addChild(_secondBtn);
			
			_otherBtn=new TabButton(LanguageManager.getWord("menu.other"),clickEvent);
			_otherBtn.x=_secondBtn.x + _secondBtn.width;
			_otherBtn.y=_secondBtn.y;
			addChild(_otherBtn);
			
			_cateringBtn=new TabButton(LanguageManager.getWord("menu.catering"),clickEvent);
			_cateringBtn.x=_otherBtn.x + _otherBtn.width;
			_cateringBtn.y=_otherBtn.y;
			addChild(_cateringBtn);
			
			_first=new FirstFoodType();
			_first.x=_firstBtn.x;
			_first.y=_firstBtn.y + _firstBtn.height - 12;
			_firstBtn.binding=_first;
			addChild(_first);
			
			_second=new SecondFoodType();
			_second.x=_first.x;
			_second.y=_first.y;
			_second.visible=false;
			_secondBtn.binding=_second;
			addChild(_second);
			
			_other=new OtherType();
			_other.x=_first.x;
			_other.y=_first.y;
			_other.visible=false;
			_otherBtn.binding=_other;
			addChild(_other);
			
			_catering=new CateringType();
			_catering.x=_first.x;
			_catering.y=_first.y;
			_catering.visible=false;
			_cateringBtn.binding=_catering;
			addChild(_catering);
			
			_remainTimetf=Utils.getTextField(Global.remainHour + LanguageManager.getWord("clock.hour") + Global.remainMin
											+ LanguageManager.getWord("clock.minute"),12);
			_remainTimetf.x=510;
			_remainTimetf.y=75;
			addChild(_remainTimetf);
			
			_cointf=Utils.getTextField(_profit + LanguageManager.getWord("menu.profit"),12);
			_cointf.x=_remainTimetf.x + 20;
			_cointf.y=_remainTimetf.y + 20;
			addChild(_cointf);
			
			initOrder();
		}
		
		
		private function initOrder():void
		{
			_orderSquare1=new OrderSquare(OrderSquare.VERTICAL,"notAddbg");
			_orderSquare1.x = _remainTimetf.x - 30;
			_orderSquare1.y = _remainTimetf.y + 50;
			_orderSquare1.close=true;
			_emptySquareArr.push(_orderSquare1);
			addChild(_orderSquare1);
			
			_orderSquare2=new OrderSquare(OrderSquare.VERTICAL,"notAddbg");
			_orderSquare2.x = _orderSquare1.x;
			_orderSquare2.y = _orderSquare1.y + _orderSquare1.height + 5;
			_orderSquare2.close=true;
			_emptySquareArr.push(_orderSquare2);
			addChild(_orderSquare2);
			
			_orderSquare3=new OrderSquare(OrderSquare.VERTICAL,"notAddbg");
			_orderSquare3.x = _orderSquare1.x;
			_orderSquare3.y = _orderSquare2.y + _orderSquare1.height + 5;
			_orderSquare3.close=true;
			_emptySquareArr.push(_orderSquare3);
			addChild(_orderSquare3);
		}
		
		
	//	private var _tmp:int=0;
		private var _tmpSquare:OrderSquare;
		/**
		 * index-初始化时位置,即不是初始化
		 * */
		public function setFoodType(source:String, num:int,index:int=-1):void
		{
			_source = source;

			if (_sourceArr.indexOf(source) != -1)
			{
				_emptySquareArr.every(checkSquare);
				_tmpSquare.foodNum = _tmpSquare.foodNum + num;
				
			}
			else
			{			
				if(_sourceArr.indexOf(null) != -1){
					if (index != -1) 
					{
						_sourceArr[index] = source;
					}else{
						_sourceArr[_sourceArr.indexOf(null)] = source;
					}
					
				}
				
				_emptySquareArr.every(checkSquare);
				_tmpSquare.firstFoodType=source;
				
				_tmpSquare.foodNum=num;

				if (_sourceArr.indexOf(null) == -1 && _sourceArr.length == 3)
				{
					Global.foodFull = true;
				}
			}
		}
		
		
		private function checkSquare(item:*,index:int,arr:Array):Boolean
		{
			if (item.source1 == _source) 
			{
				_tmpSquare=arr[index] as OrderSquare;
				return false;
			}else{
				_tmpSquare=_emptySquareArr[_sourceArr.indexOf(_source)] as OrderSquare;
				return true;
			}
		}
		
		private var _source:String;
		
		public function setFoodNum(source:String):void
		{
			_source = source;
			
			_emptySquareArr.every(checkSquare);
			
			if (_sourceArr.indexOf(source) != -1)
			{
				if (_tmpSquare.foodNum - 1 <= 0) 
				{				
					_tmpSquare.setEmptyFoodType();
					_sourceArr[_sourceArr.indexOf(source)] = null;
					_tmpSquare.foodNum=0;

					return;
				}
				_tmpSquare.foodNum = _tmpSquare.foodNum-1;
			}
			
		}
		
		private var _now:TabButton;
		
		private function clickEvent(e:MouseEvent):void
		{
			var btn:TabButton = e.target as TabButton;
			if(btn == _now) return;
			if(_now)_now.choose = false;
			btn.choose = true;
			_now = btn;
			
			switch(btn){
				case _firstBtn:
					break;
				case _secondBtn:
					break;
				case _otherBtn:
					break;
				case _cateringBtn:
					break;
			}
		}
		
		/**
		 * 将3个面板的购买按钮根据自身条件设置enabled
		 * */
		public function checkBuyBtnEnabled():void
		{
			_first.buyBtnEnabled();
			_second.buyBtnEnabled();
			_other.buyBtnEnabled();
		}
	}
}